package tlsb;

import java.io.File;

import offline.CGAXiafan;
import util.IFileReader;
import util.IPartition;
import util.RedisCluster;
import util.RedisClusterFactory;

public class LoadDataToRedis {
	private String oShipFile = "";

	public LoadDataToRedis(String fshipFile_, String bShipFile_) {
		this.oShipFile = fshipFile_;
		this.bShipFile = bShipFile_;
	}

	public void loadOwnSTL() {
		RedisCluster redisCluster = RedisClusterFactory.instance
				.getFolRedisCluster();
		IPartition part = redisCluster.getPartition();
		IFileReader ifr = new IFileReader(new File(oShipFile));
		ifr.open();
		String line = "";
		int counter = 0;
		while ((line = ifr.readLine()) != null) {
			counter++;

			String[] tmp = line.split("\t");
			if (tmp[0].endsWith(CGAXiafan.SETOFOWNEDTL)) {
				if (counter % 1000000 == 0)
					System.out.println(counter);
				String uid = tmp[0].substring(0,
						tmp[0].indexOf(CGAXiafan.SETOFOWNEDTL));
				for (int i = 1; i < tmp.length; i++)
					redisCluster.getPipeline(part.shard(uid)).sadd(tmp[0],
							tmp[i]);
			}
			if (counter % 100 == 0)
				redisCluster.synAllPipelines();
		}
		redisCluster.synAllPipelines();
		System.out.println("followship counter:" + counter);
		// System.out.println("B counter:"+jedis.scard(ALLUIDS));
		ifr.close();
		redisCluster.close();
	}

	private String bShipFile = "";

	public void loadBelongedSTL() {
		RedisCluster redisCluster = RedisClusterFactory.instance
				.getFolRedisCluster();
		IPartition part = redisCluster.getPartition();
		IFileReader ifr = new IFileReader(new File(bShipFile));
		ifr.open();
		String line = "";
		int counter = 0;
		while ((line = ifr.readLine()) != null) {
			counter++;

			String[] tmp = line.split("\t");
			if (tmp[0].endsWith(CGAXiafan.SETOFBELONGEDTL)) {
				if (counter % 1000000 == 0)
					System.out.println(counter);
				String uid = tmp[0].substring(0,
						tmp[0].indexOf(CGAXiafan.SETOFBELONGEDTL));
				redisCluster.getPipeline(part.shard(uid)).sadd(tmp[0], tmp[1]);
			} else {
				String sid = tmp[0].substring(0, tmp[0].length() - 1);
				System.out.println(tmp[0].substring(0, tmp[0].length() - 1));
				for (int i = 1; i < tmp.length; i++) {
					redisCluster.getPipeline(part.shard(tmp[i])).sadd(
							Utils.joinStr(tmp[i], CGAXiafan.SETOFBELONGEDTL),
							sid);
				}
			}
			if (counter % 100 == 0)
				redisCluster.synAllPipelines();
		}
		redisCluster.synAllPipelines();
		System.out.println("followship counter:" + counter);
		// System.out.println("B counter:"+jedis.scard(ALLUIDS));
		ifr.close();
		redisCluster.close();
	}

	public static void main(String[] args) {
		System.out.println("own, belong");
		// LoadDataToRedis load = new LoadDataToRedis(
		// "/Volumes/wjx/模拟数据集/own.txt",
		// "/Volumes/wjx/模拟数据集/stl_mapped.txt");
		LoadDataToRedis load = new LoadDataToRedis(args[0], args[1]);
		load.loadBelongedSTL();
		load.loadOwnSTL();
		/*
		 * IFileReader reader = new IFileReader(new
		 * File(Configuration.instance() .getReadSequenceFile()));
		 * reader.open(); RedisCluster folRedisCluster =
		 * RedisClusterFactory.instance .getFolRedisCluster(); for (int i = 0; i
		 * < 10000; i++) { String uid = reader.readLine().split("\t")[0];
		 * System.out.print(uid + " ");
		 * System.out.println(folRedisCluster.getJedis(
		 * folRedisCluster.getPartition().shard(uid)).scard( Utils.joinStr(uid,
		 * CGAXiafan.SETOFOWNEDTL))); } reader.close();
		 */
	}
}
